home *** CD-ROM | disk | FTP | other *** search
- Path: news.onramp.net!usenet
- From: Lee Meador <rtbs@onramp.net>
- Newsgroups: comp.lang.c
- Subject: Re: string search?
- Date: Wed, 27 Mar 1996 10:02:30 -0800
- Organization: RealTime
- Message-ID: <315982B6.2113@onramp.net>
- References: <4jc6ae$eol@voyager.eng.gulfaero.com> <4jdot7$eb0@sparcserver.lrz-muenchen.de> <828126918snz@genesis.demon.co.uk>
- NNTP-Posting-Host: stemmons31.onramp.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
- CC: Ricardo Mor <rmor1@ix.netcom.com>
-
- A bunch of people wrote how:
- > >>Ricardo Mor <rmor1@ix.netcom.com> wrote:
- > >>> How can I search for a string of characters within another string?
-
- char *p;
-
- str1 = "TIME";
- str2 = "NEXT_TIME";
-
- for (p = str2; (*p); ++p) {
- if (*p == *str1 && strcmp(p, str1) == 0) {
- /* It matches */
- break;
- }
- }
- if (*p) {
- /* It doesn't match */
- }
-
- NOTE: You could put in an inner loop and write your own 'strcmp' if you like
- to avoid function calls altogether.
-
- NOTE2: This is all best put into a function that we could call something
- clever. Say, we could call it 'strstr'. It would take str1 and str2 as
- parameters and return a pointer to the matching point or NULL if no match.
-
- -- Lee Meador
-